package com.jh.concurrent.base.status;

/**
 * @author JH_Wong
 * @projectName newforward
 * @title ThreadStatusTest.java
 * @package com.jh.concurrent.base.status
 * @description 线程状态测试类
 * @date 2019-05-09 15:59
 */
public class ThreadStatusTest {

    public static void main(String[] args) {

        // TimeWaiting
        new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "TimeWaiting_Thread").start();

        // Waiting
        new Thread(() -> {
            synchronized (ThreadStatusTest.class){
                while (true){
                    try {
                        ThreadStatusTest.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "Waiting_Thread").start();

        /**
         * 测试阻塞
         */
        new Thread(new BlockDemo(), "Block1").start();
        new Thread(new BlockDemo(), "Block2").start();

    }

    static class BlockDemo extends Thread {
        @Override
        public void run() {
            while (true){
                synchronized (BlockDemo.class) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
